//app.js
App({
  onLaunch: function() {
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate(function (res) {
        if (res.hasUpdate) {
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，是否重启应用？',
              success: function (res) {
                if (res.confirm) {
                  updateManager.applyUpdate()
                }
              }
            })
          });
          updateManager.onUpdateFailed(function () {
            wx.showModal({
              title: '新版本已经准备好',
              content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~'
            })
          })
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    };
    // //检测登录是否过期
    // wx.checkSession({
    //   success(){
    //     console.log("未过期");
    //   },
    //   fail(){
    //     console.log("已过期");
    //     wx.clearStorage();
    //   }
    // });
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  //第一种底部  总部  
  editTabBar: function() {
    //使用getCurrentPages可以获取当前加载中所有的页面对象的一个数组，数组最后一个就是当前页面。
    var curPageArr = getCurrentPages(); //获取加载的页面
    var curPage = curPageArr[curPageArr.length - 1]; //获取当前页面的对象
    var pagePath = curPage.route; //当前页面url
    if (pagePath.indexOf('/') != 0) {
      pagePath = '/' + pagePath;
    }

    var tabBar = this.globalData.tabBar;
    for (var i = 0; i < tabBar.list.length; i++) {
      tabBar.list[i].active = false;
      if (tabBar.list[i].pagePath == pagePath) {
        tabBar.list[i].active = true; //根据页面地址设置当前页面状态    
      }
    }
    curPage.setData({
      tabBar: tabBar
    });
  },

  //第二种底部，原理同上   分院
  editTabBar1: function() {
    var curPageArr = getCurrentPages();
    var curPage = curPageArr[curPageArr.length - 1];
    var pagePath = curPage.route;
    if (pagePath.indexOf('/') != 0) {
      pagePath = '/' + pagePath;
    }
    var tabBar = this.globalData.tabBar1;
    // console.log(this.globalData.tabBar1)
    for (var i = 0; i < tabBar.list.length; i++) {
      tabBar.list[i].active = false;
      if (tabBar.list[i].pagePath == pagePath) {
        tabBar.list[i].active = true;
      }
    }
    curPage.setData({
      tabBar: tabBar
    });
  },


  //第三种底部，原理同上    用户
  editTabBar2: function() {
    var curPageArr = getCurrentPages();
    var curPage = curPageArr[curPageArr.length - 1];
    var pagePath = curPage.route;
    if (pagePath.indexOf('/') != 0) {
      pagePath = '/' + pagePath;
    }
    var tabBar = this.globalData.tabBar2;
    // console.log(this.globalData.tabBar1)
    for (var i = 0; i < tabBar.list.length; i++) {
      tabBar.list[i].active = false;
      if (tabBar.list[i].pagePath == pagePath) {
        tabBar.list[i].active = true;
      }
    }
    curPage.setData({
      tabBar: tabBar
    });
  },

  //第四种底部，原理同上   业务员
  editTabBar3: function() {
    var curPageArr = getCurrentPages();
    var curPage = curPageArr[curPageArr.length - 1];
    var pagePath = curPage.route;
    if (pagePath.indexOf('/') != 0) {
      pagePath = '/' + pagePath;
    }
    var tabBar = this.globalData.tabBar3;
    // console.log(this.globalData.tabBar1)
    for (var i = 0; i < tabBar.list.length; i++) {
      tabBar.list[i].active = false;
      if (tabBar.list[i].pagePath == pagePath) {
        tabBar.list[i].active = true;
      }
    }
    curPage.setData({
      tabBar: tabBar
    });
  },

  globalData: {
    //第一种底部导航栏显示
    tabBar: {
      "color": "#000",
      "selectedColor": "#ccc",
      "backgroundColor": "#fff",
      "list": [{
          "pagePath": "/pages/Generalhospital/homepage/homepage",
          "text": "概览",
          "iconPath": "/image/gailang1.png",
          "selectedIconPath": "/image/gailang.png",
          "clas": "menu-item1",
          "active": false
        },
        {
          "pagePath": "/pages/Generalhospital/Branch/Branch",
          "text": "分院",
          "iconPath": "/image/fengyuan1.png",
          "selectedIconPath": "/image/fengyuan.png",
          "clas": "menu-item1",
          "active": false
        },
        {
          "pagePath": "/pages/Generalhospital/curriculum/curriculum",
          "text": "课程",
          "iconPath": "/image/class.png",
          "selectedIconPath": "/image/class1.png",
          "clas": "menu-item1",
          "active": false
        }
      ],
      "position": "bottom"
    },
    //分院导航
    tabBar1: {
      "color": "#ccc",
      "selectedColor": "#ccc",
      "backgroundColor": "#ffffff",
      "list": [{
          "pagePath": "/pages/branch/homepage/homepage",
          "text": "概览",
          "iconPath": "/image/gailang1.png",
          "selectedIconPath": "/image/gailang.png",
          "clas": "menu-item1",
          "selectedColor": "#4EDF80",
          "active": false
        },
        {
          "pagePath": "/pages/branch/curriculum/curriculum",
          "text": "课程",
          "iconPath": "/image/class.png",
          "selectedIconPath": "/image/class1.png",
          "selectedColor": "#4EDF80",
          "clas": "menu-item1",
          "active": true
        },
        {
          "pagePath": "/pages/branch/me/me",
          "text": "我的",
          "iconPath": "/image/me.png",
          "selectedIconPath": "/image/me1.png",
          "selectedColor": "#4EDF80",
          "clas": "menu-item1",
          "active": true
        }
      ],
      "position": "bottom"
    },
    //业务员
    tabBar3: {
      "color": "#ccc",
      "selectedColor": "#ccc",
      "backgroundColor": "#ffffff",
      "list": [{
          "pagePath": "/pages/Salesman/curriculum/curriculum",
          "text": "课程",
          "iconPath": "/image/class.png",
          "selectedIconPath": "/image/class1.png",
          "clas": "menu-item1",
          "selectedColor": "#4EDF80",
          "active": false
        },
        {
          "pagePath": "/pages/Salesman/me/me",
          "text": "我的",
          "iconPath": "/image/me.png",
          "selectedIconPath": "/image/me1.png",
          "selectedColor": "#4EDF80",
          "clas": "menu-item1",
          "active": true
        }
      ],
      "position": "bottom"
    },
    //用户导航
    tabBar2: {
      "color": "#ccc",
      "selectedColor": "#ccc",
      "backgroundColor": "#ffffff",
      "list": [{
          "pagePath": "/pages/User/homepage/homepage",
          "text": "课程",
          "iconPath": "/image/class.png",
          "selectedIconPath": "/image/class1.png",
          "clas": "menu-item1",
          "selectedColor": "#4EDF80",
          "active": false
        },
        {
          "pagePath": "/pages/User/me/me",
          "text": "我的",
          "iconPath": "/image/me.png",
          "selectedIconPath": "/image/me1.png",
          "selectedColor": "#4EDF80",
          "clas": "menu-item1",
          "active": true
        }
      ],
      "position": "bottom"
    }
  },
  // globalData: {
  //   userInfo: null
  // }
})